package com.ruoyi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.domain.ParkingInfo;
import com.ruoyi.dto.dahua.DaHuaIncomeDetailsDto;
import com.ruoyi.dto.dahua.DaHuaPageQueryDto;
import com.ruoyi.mapper.ParkingInfoMapper;
import com.ruoyi.mapper.ParkingOrderMapper;
import com.ruoyi.service.IDaHuaDigitalService;
import com.ruoyi.utils.mapper.DaHuaDigitalMapper;
import com.ruoyi.utils.MapListUtil;
import com.ruoyi.vo.dahua.DaHuaIncomeDetailsVo;
import com.ruoyi.vo.dahua.DaHuaIncomeOverviewVo;
import com.ruoyi.vo.dahua.DaHuaParkingListVo;
import com.ruoyi.vo.dahua.DaHuaResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 大华数字孪生大屏Service业务层处理
 * 
 * @author dongdong
 * @date 2025-09-08
 */
@Slf4j
@Service
public class DaHuaDigitalServiceImpl implements IDaHuaDigitalService {

    @Resource
    private ParkingOrderMapper parkingOrderMapper;
    @Resource
    private ParkingInfoMapper parkingInfoMapper;

    @Override
    public DaHuaResponse getParkingList(DaHuaPageQueryDto daHuaPageQueryDto) {
        log.info("数字大屏停车场列表 请求报文: {}", daHuaPageQueryDto);
        try {
            // 设置分页参数
            PageHelper.startPage(daHuaPageQueryDto.getPageNum(), daHuaPageQueryDto.getPageSize());

            // 查询数据列表
            List<ParkingInfo> parkingList = parkingInfoMapper.selectParkingInfoList(new ParkingInfo());
            PageInfo<ParkingInfo> pageInfo = new PageInfo<>(parkingList);
            List<DaHuaParkingListVo> list = DaHuaDigitalMapper.INSTANCE.toDaHuaParkingListVo(parkingList);

            // 使用您的泛型返回类
            return DaHuaResponse.successPage(
                    list,
                    pageInfo.getPageNum(),      // 当前页
                    (int) pageInfo.getTotal(),  // 总记录数
                    pageInfo.getPages()         // 总页数
            );

        } catch (Exception e) {
            DaHuaResponse response = new DaHuaResponse<>();
            response.setSuccess(false);
            response.setCode(DaHuaResponse.FAILED_CODE);
            response.setErrMsg("查询失败: " + e.getMessage());
            return response;
        } finally {
            // 清理分页线程变量
            PageHelper.clearPage();
        }
    }

    @Override
    public DaHuaResponse getIncomeDetails(DaHuaIncomeDetailsDto daHuaIncomeDetailsDto) {
        log.info("数字大屏停车场收入明细 请求报文: {}", daHuaIncomeDetailsDto);
        try {
            // 设置分页参数
            PageHelper.startPage(daHuaIncomeDetailsDto.getPageNum(), daHuaIncomeDetailsDto.getPageSize());

            // 查询数据列表
            List<DaHuaIncomeDetailsVo> parkingOrderList = parkingOrderMapper.selectIncomeDetails(daHuaIncomeDetailsDto);
            PageInfo<DaHuaIncomeDetailsVo> pageInfo = new PageInfo<>(parkingOrderList);

            // 使用您的泛型返回类
            return DaHuaResponse.successPage(
                    parkingOrderList,
                    pageInfo.getPageNum(),      // 当前页
                    (int) pageInfo.getTotal(),  // 总记录数
                    pageInfo.getPages()         // 总页数
            );

        } catch (Exception e) {
            DaHuaResponse response = new DaHuaResponse<>();
            response.setSuccess(false);
            response.setCode(DaHuaResponse.FAILED_CODE);
            response.setErrMsg("查询失败: " + e.getMessage());
            return response;
        } finally {
            // 清理分页线程变量
            PageHelper.clearPage();
        }
    }

    @Override
    public DaHuaResponse getIncomeOverview(Long parkingId) {
        String yesterday = LocalDate.now().minusDays(1).toString();
        List<Map<String, Object>> list = parkingOrderMapper.selectIncomeOverview(parkingId, yesterday);
        // 今日路内停车场总收入
        double todayRoadSideTotalMoney = MapListUtil.sumByConditions(list, "0", "2");
        // 今日路外停车场总收入
        double todayParkTotalMoney = MapListUtil.sumByConditions(list, "0", "1");
        // 昨日路内停车场总收入
        double yesterdayRoadSideTotalMoney = MapListUtil.sumByConditions(list, "1", "2");
        // 昨日路外停车场总收入
        double yesterdayParkTotalMoney = MapListUtil.sumByConditions(list, "1", "1");
        DaHuaIncomeOverviewVo vo = new DaHuaIncomeOverviewVo();
        vo.setTodayTotalMoney(todayRoadSideTotalMoney + todayParkTotalMoney);
        vo.setYesterdayTotalMoney(yesterdayRoadSideTotalMoney + yesterdayParkTotalMoney);
        vo.setTodayRoadSideTotalMoney(todayRoadSideTotalMoney);
        vo.setYesterdayRoadSideTotalMoney(yesterdayRoadSideTotalMoney);
        vo.setTodayParkTotalMoney(todayParkTotalMoney);
        vo.setYesterdayParkTotalMoney(yesterdayParkTotalMoney);
        return DaHuaResponse.success(vo);
    }

    @Override
    public DaHuaResponse getParkingLot(Long parkingId) {
        List<DaHuaParkingListVo> list = new ArrayList<>();
        if (Objects.isNull(parkingId)){
            List<ParkingInfo> parkingList = parkingInfoMapper.selectParkingInfoList(new ParkingInfo());
            list = DaHuaDigitalMapper.INSTANCE.toDaHuaParkingListVo(parkingList);
        }else {
            ParkingInfo parkingInfo = parkingInfoMapper.selectParkingInfoById(parkingId);
            DaHuaParkingListVo vo = DaHuaDigitalMapper.INSTANCE.toDaHuaParkingVo(parkingInfo);
            list.add(vo);
        }
        return DaHuaResponse.success(list);
    }

}
